package com.cnsuning.jobtool.jobtools.cdc;

import com.cnsuning.jobtool.utils.ExcelUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CDCGenerator {

    /**生成文件目标文件夹*/
    private String targetFilePath = "src/main/java/com/cnsuning/jobtool/jobtools/cdc/files/generate";
    /**配置文件目录*/
    private String configFilePath = "src/main/java/com/cnsuning/jobtool/jobtools/cdc/cdc_generate_config.xlsx";
    private String YD1_CONFIG_FILE = "src/main/java/com/cnsuning/jobtool/jobtools/cdc/files/YD1.txt";
    private String YD2_CONFIG_FILE = "src/main/java/com/cnsuning/jobtool/jobtools/cdc/files/YD2.txt";
    private String YD3_CONFIG_FILE = "src/main/java/com/cnsuning/jobtool/jobtools/cdc/files/YD3.txt";
    private String YD4_CONFIG_FILE = "src/main/java/com/cnsuning/jobtool/jobtools/cdc/files/YD4.txt";



    private Map<String,ConfigBean> readConfigFile() throws IOException, InvalidFormatException {
        List<LinkedHashMap<String, Object>> result = ExcelUtils.readExcelHasTitleOneSheet(new File(configFilePath), "Sheet1");
        Map<String,ConfigBean> configMap = null;
        if (!CollectionUtils.isEmpty(result)){
            configMap = new HashMap<>();
            for (LinkedHashMap<String, Object> rowMap: result)
            {
                String ac = (String) rowMap.get("AC");
                String acPort = (String) rowMap.get("ACPORT");
                String acUsername = (String) rowMap.get("ACUSERNAME");
                String acPassword = (String) rowMap.get("ACPASS");
                String srcSchema = (String) rowMap.get("srcSchema");
                String targetSchema = (String) rowMap.get("targetSchema");
                String sourceDatastore = (String) rowMap.get("db2数据存储器名称");
                String targetDatastore = (String) rowMap.get("mysql数据存储器名称");
                String subscriptionCollect = (String) rowMap.get("预定名称");

                if (!StringUtils.isEmpty(subscriptionCollect))
                {
                    if (subscriptionCollect.contains(",")) {
                        String[] split = subscriptionCollect.split(",");
                        for (String subscription:split)
                        {
                            ConfigBean configBean = new ConfigBean(ac,acPort,acUsername,acPassword,sourceDatastore,targetDatastore,srcSchema,targetSchema);
                            configBean.setSubscription(subscription);
                            configMap.put(subscription,configBean);
                        }
                    }else{
                        ConfigBean configBean = new ConfigBean(ac,acPort,acUsername,acPassword,sourceDatastore,targetDatastore,srcSchema,targetSchema);
                        configBean.setSubscription(subscriptionCollect);
                        configMap.put(subscriptionCollect,configBean);
                    }

                }
            }
        }


        return configMap;
    }

    @Test
    public void generate() throws IOException, InvalidFormatException, IllegalAccessException {
        //清空目标文件夹
        FileUtils.deleteDirectory(new File(targetFilePath));

        Map<String, ConfigBean> configBeanMap = readConfigFile();
        if (configBeanMap == null || configBeanMap.isEmpty())
            throw new IllegalAccessException("读取配置文件出错，请检查！");
        //读取脚本母版
        String yd1 = FileUtils.readFileToString(new File(YD1_CONFIG_FILE), "utf-8");
        String yd2 = FileUtils.readFileToString(new File(YD2_CONFIG_FILE), "utf-8");
        String yd3 = FileUtils.readFileToString(new File(YD3_CONFIG_FILE), "utf-8");
        String yd4 = FileUtils.readFileToString(new File(YD4_CONFIG_FILE), "utf-8");

        for (Map.Entry<String,ConfigBean> entry : configBeanMap.entrySet())
        {
            String key = entry.getKey();
            ConfigBean value = entry.getValue();
            String tempString="";
            if (key.endsWith("1"))
            {
                tempString = yd1;
            }
            else if (key.endsWith("2"))
            {
                tempString = yd2;
            }
            else if (key.endsWith("3"))
            {
                tempString = yd3;
            }
            else if (key.endsWith("4"))
            {
                tempString = yd4;
            }
            String result = tempString.replace("${ACCESS_HOSTNAME}", value.getAccessHostname())
                    .replace("${ACCESS_PORT}", value.getAccessPort())
                    .replace("${ACCESS_USERNAME}", value.getAcUsername())
                    .replace("${ACCESS_PASSWORD}", value.getAcPassword())
                    .replace("${SOURCE_DATASTORE}", value.getSourceDatastore())
                    .replace("${TARGET_DATASTORE}", value.getTargetDatastore())
                    .replace("${SUBSCRIPTION}", value.getSubscription())
                    .replace("${sourceSchema}", value.getSourceSchema())
                    .replace("${targetSchema}", value.getTargetSchema());
            FileUtils.writeStringToFile(new File(targetFilePath+File.separator+value.getAccessHostname()+File.separator+key+".txt"),result,"utf-8",false);
        }
    }
}
